{{extend 'layout.html'}}
<script><!--
$(document).ready(function(){
  $("table.sortable tbody tr").mouseover( function() {
     $(this).addClass("highlight"); }).mouseout( function() { 
     $(this).removeClass("highlight"); });
  $('table.sortable tbody tr:odd').addClass('odd');
  $('table.sortable tbody tr:even').addClass('even');

});
//--></script>

{{try:}}{{=uplink}}{{except:}}{{pass}}
<h1>
{{if request.function=='state':}}
 Internal state
{{else:}}
  {{if len(request.args)>=1:}}
    database {{=A(request.args[0],_href=URL(r=request,f='index/'))}}
    {{try:}}{{query}}{{except:}}{{query=None}}{{pass}}
    {{if not query:}}table {{=A(request.args[1],_href=URL(r=request,f='select/%s/%s'% tuple(request.args[:2])))}} {{=request.function}} {{else:}} generic select/update/delete{{pass}}
    {{if len(request.args)==3:}}
      record id {{=A(request.args[2],_href=URL(r=request,f='update/%s/%s/%s'% tuple(request.args[:3])))}}
    {{pass}}
  {{else:}}
    Available databases and tables
  {{pass}}
{{pass}}
</h1>


{{if request.function=='index':}}
  {{if len(dbs)==0:}}No databases in this application{{pass}}
  {{for dbname,items in dbs.items():}}
    {{for dt in items:}}
      <h2>{{=A("%s.%s"%dt,_href=URL(r=request,f='select/%s/%s'%dt))}}</h2>
      [ {{=A('insert new '+dt[1],_href=URL(r=request,f='insert/%s/%s'%dt))}} ]
      <br/><br/>
    {{pass}}
  {{pass}}<br/><br/>
{{pass}}

{{if request.function=='select' and len(request.args)>1:}}
  [ {{=A('insert new '+request.args[1],_href=URL(r=request,f='insert/%s/%s'%tuple(request.args[:2])))}} ]<br/><br/>
    <h2>Rows in table</h2><br/>
{{elif request.function=='select' and len(request.args)==1:}}
    <h2>Rows selected</h2><br/>
{{pass}}
{{if request.function=='select' and len(request.args)>=1:}}
    <form action="{{=URL(r=request,args=request.args[:1])}}" method="post">
    <table>
    <tr><td width="150px">SQL FILTER:</td><td><input type="text" value="{{=query}}" name="query" size="60"/><input type="submit" value="apply"/></td></tr>
    {{if len(request.args)==1:}}
    <tr><td>UPDATE STRING:</td><td><input type="text" value="" name="update_string"/> or DELETE ALL: <input type="checkbox" name="delete_all"/> (sure?<select name="delete_all_sure"/><option selected>no</option><option>yes</option></select>)<br/><i>(The SQL FILTER is a condition like "table1.field1='value'". Something like "table1.field1=table2.field2" results in a SQL JOIN. Use AND, OR and (...) to build more complex filters. The UPDATE STRING is an optional expression like "field1='newvalue'". You cannot update or delete the results of a JOIN)</i>

</td></tr>
    {{pass}}
    </table>
    </form>
    <br/>
    {{if start>0:}}[ {{=A('previous 100 records',_href=URL(r=request,f='select/%s?start=%s'%('/'.join(request.args),start-100)))}} ]{{pass}}
    {{if nrecords==100:}}[ {{=A('next 100 records',_href=URL(r=request,f='select/%s?start=%s'%('/'.join(request.args),start+100)))}} ]{{pass}}
<div style="overflow: auto;" width="80%">
    {{=records}}
</div>
     <br/><br/><h2>Import/Export</h2><br/>
     [ <a href="{{=URL(r=request,f='csv',vars=dict(dbname=request.args[0],query=query))}}">export as csv file</a> ]
  {{if len(request.args)==2:}}
     {{=FORM('or import from csv file ',INPUT(_type='file',_name='csvfile'),INPUT(_type='submit',_value='import'))}}
  {{pass}}
{{pass}}

{{if request.function=='insert' and len(request.args)>1:}}  
  <h2>New Record</h2><br/>
  {{=form}}
{{pass}}

{{if request.function=='update' and len(request.args)>2:}}
  <h2>Edit current record</h2><br/><br/>{{=form}}
{{pass}}

{{if request.function=='state':}}
  <h2>Current request</h2>
  {{=BEAUTIFY(request)}}
  <br/><h2>Current response</h2>
  {{=BEAUTIFY(response)}}
  <br/><h2>Current session</h2>
  {{=BEAUTIFY(session)}}
{{pass}}